<?php
/**
 * 菜单管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
UE_Loader::loadModel('MenuModel', 'system');
class MenuController extends UE_Controller_Admin {
	private $_menu = array();
	
	public function init() {
	    $menu = array();
		$sql = "SELECT * FROM ue_menu ORDER BY menu_order ASC";
		$rs = UE_DB::factory()->getAll($sql);
		
		$t = new UE_Tree();
		$t->set($rs, 'menu_id', 'menu_parent_id');
		$menu = $t->get();
		$this->_menu = $menu;
	}
	public function listAction() {
	    if (IS_POST && !empty($_POST['menu_order'])) {
	    	//print_r($_POST['menu_order']);
	    	foreach ($_POST['menu_order'] as $key=>$val) {
	    		$sql = "UPDATE ue_menu SET menu_order='$val' WHERE menu_id=$key";
	    		UE_DB::factory()->query($sql);
	    	}
	    	
	    	$this->init();
	    }
	    
		include $this->tpl();
	}
	
	public function addAction() {
		if (IS_POST) {
			$menu = new MenuModel();
			if($menu->add($_POST)){
				$this->setOK('成功添加菜单');
				$this->init();
			} else {
				$this->setErr($menu->getErrs());
			}
		}
		
	    include $this->tpl();
	}
	
	public function editAction() {
		if (!$this->id) {
			print 'Error Param';
			return ;
		}
		
		$menu = new MenuModel();		
		
		if (IS_POST) {
			if($menu->setId($this->id)->update($_POST)){
				$this->setOK('成功编辑菜单');
			} else {
				$this->setErr($menu->getErrs());
			}
		}
		
		$item = $menu->setId($this->id)->read();
		
	    include $this->tpl();
	}
	
	/**
	 * 删除菜单
	 *
	 */
	public function delAction() {
	    if (empty($this->id)) {
	    	print 'Error Param';
	    	return ;
	    }
	    $menu = new MenuModel();
	    $menu->setId($this->id)->delete();
	    header('Location:'.UE::referer());
	}
}